#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

start_pod

names = Set.new %w[
  MASTER_FLUENTD_HOST
  MASTER_FLUENTD_PORT
]

defaults = relevant_defaults(names)
defaults['MASTER_FLUENTD_PORT'] ||= '24224'
env = docker_env(defaults)
raise 'must config MASTER_FLUENTD_HOST' unless defaults['MASTER_FLUENTD_HOST']

docker_rm 'sub-fluentd'

cmd = %w[
  docker run
  --restart=always
  --name sub-fluentd
  --network host
  -v /etc/localtime:/etc/localtime:ro
  -v /srv/cci/serial/logs:/srv/cci/serial/logs:ro
  -v /srv/cci/serial/fluentd-pos:/srv/cci/serial/fluentd-pos
  -d
] + env + %w[
  -u 1090:1090
  -p 24225:24224/tcp
  -p 24225:24224/udp
  -e FLUENTD_CONF=docker-fluentd.conf
  --log-driver json-file
  --log-opt max-size=1g
  sub-fluentd:alpine
]
cmd += ['bash', '-c', 'umask 027 & fluentd -c /fluentd/etc/docker-fluentd.conf']

system(*cmd)
